import sys
from PyQt5.QtWidgets import QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem, QHeaderView, \
	QAbstractItemView, QTableView, QComboBox, QPushButton


class Table(QWidget):
	def __init__(self):
		super(Table, self).__init__()
		self.initUI()

	def initUI(self):
		self.setWindowTitle("QTableWidget 例子")
		self.resize(400, 300)
		conLayout = QHBoxLayout()
		tableWidget = QTableWidget(4,3)
		# tableWidget.setRowCount(4)
		# tableWidget.setColumnCount(3)
		conLayout.addWidget(tableWidget)
		tableWidget.setHorizontalHeaderLabels(['姓名', '性别', '体重(kg)'])
		# tableWidget.verticalHeader().setVisible(False)
		# tableWidget.horizontalHeader().setVisible(False)
		tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

		tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
		tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)


		newItem = QTableWidgetItem('张三')
		tableWidget.setItem(0, 0, newItem)

		comBox = QComboBox()
		comBox.addItem('男')
		comBox.addItem('女')
		comBox.setStyleSheet("QComboBox{margin:3px};")
		tableWidget.setCellWidget(0,1,comBox)

		searchBtn = QPushButton('修改')
		searchBtn.setDown(True)
		searchBtn.setStyleSheet("QPushButton(margin:3px};")
		tableWidget.setCellWidget(0,2,searchBtn)

		# tableWidget.horizontalHeader().setSelectionResizeMode(QHeaderView.ResizeToContents)

		self.setLayout(conLayout)



if __name__ == '__main__':
	app = QApplication(sys.argv)
	example = Table()
	example.show()
	sys.exit(app.exec_())
